package com.changgou.controller;

import com.changgou.file.FastDFSFile;
import com.changgou.util.FastDFSClient;
import entity.Result;
import entity.StatusCode;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@CrossOrigin
public class FileController {
    /**
     * 文件上传
     */
    @PostMapping(value = "/upload")
    public Result upload(@RequestParam(value = "file")MultipartFile file) throws Exception{
        //封装一个FastDFSFile
        FastDFSFile fastDFSFile = new FastDFSFile(
          file.getOriginalFilename(),//文件名
          file.getBytes(), //文件字节数组
                StringUtils.getFilenameExtension(file.getOriginalFilename())
        );
        //文件上传
        String[] uploads = FastDFSClient.upload(fastDFSFile);
        //http://192.168.211.132:8080/group1/M00/00/00/wKjThF0DBzaAP23MAAXz2mMp9oM26.jpeg
        String url = "http://192.168.211.132:8080/"+uploads[0]+"/"+uploads[1];
        return new Result(true, StatusCode.OK,"上传成功",url);
    }
}
